package com.elesyser.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.elesyser.R;
import com.elesyser.util.ScoreManager;
import com.elesyser.util.ScoreInfo;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class ScoreActivity extends Activity {

	ScoreManager score;
	private ProgressBar progress;
	private ListView scorelist;
	private Spinner spinner1, spinner2;
	private ArrayAdapter adapter1, adapter2;
	private Button search;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.score);
    	progress = (ProgressBar) findViewById(R.id.score_progress);
    	//找到Xml中定义的下拉列表
        spinner1 = (Spinner)findViewById(R.id.schoolyear);
        spinner2 = (Spinner)findViewById(R.id.term);
        //准备一个数组适配器
        adapter1 = ArrayAdapter.createFromResource(this, R.array.schoolyear, android.R.layout.simple_spinner_item);
        adapter2 = ArrayAdapter.createFromResource(this, R.array.term, android.R.layout.simple_spinner_item);
        //设置下拉样式
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //为下拉列表设置适配器
        spinner1.setAdapter(adapter1);
        spinner2.setAdapter(adapter2);
        
        search = (Button) findViewById(R.id.bt_searchscore);
        search.setOnClickListener(SearchListener);
    }
	
	public OnClickListener SearchListener = new OnClickListener() {
		@Override
		public void onClick(View arg0) {
			new getScore().execute(spinner1.getSelectedItem().toString(), spinner2.getSelectedItem().toString());
		}
	};
	
	private class getScore extends AsyncTask<String,Integer,List<ScoreInfo>>{
		@Override
		protected void onPreExecute() {
			progress.setVisibility(View.VISIBLE);
		}
		
		@Override
		protected void onPostExecute(List<ScoreInfo> result){
			progress.setVisibility(View.GONE);
			if(result!=null){
				List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
				for(int i = 0; i < result.size(); ++i){
					Map<String, Object> ret = new HashMap<String, Object>();
					ret.put("tv_score_coursename", result.get(i).getCourseName());
					ret.put("tv_scorevalue", result.get(i).getScore());
					ret.put("tv_credit", result.get(i).getCredit());
					ret.put("tv_scorename", result.get(i).getScorename());
					ret.put("tv_maxscore", result.get(i).getMax());
					data.add(ret);
				}
				setContentView(R.layout.scorelist);
				scorelist = (ListView) findViewById(R.id.lv_score);
				scorelist.setAdapter(new SimpleAdapter(ScoreActivity.this,data,R.layout.scoreitem,new String[]{"tv_score_coursename","tv_scorevalue","tv_credit","tv_scorename","tv_maxscore"}, new int[]{R.id.tv_score_coursename,R.id.tv_scorevalue,R.id.tv_credit,R.id.tv_scorename,R.id.tv_maxscore}));
			}
			else{
				Toast.makeText(ScoreActivity.this, "没有符合条件的考试成绩记录",
						Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		protected List<ScoreInfo> doInBackground(String... params) {
			score = new ScoreManager( params[0], params[1]);
			List<ScoreInfo> ret = score.getScores();
			return ret;
		}
    }
}
